#include <tsin/library/BulletKeepAliveEvent.hpp>

#include <tsin/runtime/GameRuntime.hpp>
#include <tsin/runtime/BulletRuntime.hpp>

#include <tsin/library/GameViewport.hpp>

#include <tsin/util/Logger.hpp>

#include <cstring>
#define _USE_MATH_DEFINES
#include <cmath>

using namespace tsin;

BulletKeepAliveEvent::BulletKeepAliveEvent(bool d)
        : BulletEvent(d), timeout(0)
{
}

DynamicObject *BulletKeepAliveEvent::copy()
{
        return DynamicObject::copy(new BulletKeepAliveEvent(true));
}

void BulletKeepAliveEvent::init(int timeout)
{
        this->timeout = timeout;
}

void BulletKeepAliveEvent::reinit()
{
        int arg1 = arguments[0]->getInt();

        switch(nrArguments) {
        case 0:
                init();
                break;
        case 1:
                if (strcmp(desp, "N") == 0)
                        init(arg1);
                break;
        }
}

bool BulletKeepAliveEvent::trigger(BulletRuntime &bullet, void *)
{
        return bullet.stage == BULLET_STAGE_LIVE;
}

BulletEvent *BulletKeepAliveEvent::handle(BulletRuntime &bullet, void *)
{
        if (timeout <= 0)
                return next();

        timeout --;
        return this;
}

DynamicObject *bindKeepAliveEvent()
{
        return new BulletKeepAliveEvent(true);
}
